package com.itcast.activiti;

import com.alibaba.fastjson.JSON;
import com.itcast.activiti.utils.SecurityUtil;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.task.model.Task;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.model.payloads.CompleteTaskPayload;
import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.Map;

/**
 * activiti7新特性
 */
@RunWith(SpringRunner.class)
@SpringBootTest
class ActivitiTest2 {
    @Autowired
    private ProcessRuntime processRuntime;
    @Autowired
    private TaskRuntime taskRuntime;
    @Autowired
    private SecurityUtil securityUtil;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private TaskService taskService;


    /**
     * 原生activiti手动部署流程
     */
    @Test
    public void deploy() {
        String bpmnName = "pmis_leave_business";
        String name = "PMIS请假审批流程";
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment().name(name);
        Deployment deployment = deploymentBuilder
                .addClasspathResource("processes/" + bpmnName + ".bpmn")
                .deploy();

        if (deployment != null) {
            System.out.println("原生activiti部署流程：" + JSON.toJSON(deployment));
            Map<String, Object> result = new HashMap<>(2);
            result.put("deployID", deployment.getId());
            result.put("deployName", deployment.getName());
            System.out.println(JSON.toJSON(result));
        }
    }

    /**
     * 启动流程
     */
    @Test
    public void startInstance() {
        securityUtil.logInAs("salaboy");
        String businessKey = "demo1001";
        ProcessInstance processInstance = processRuntime.start(
                ProcessPayloadBuilder
                        .start()
                        .withProcessDefinitionKey("demo")
                        .withName("demo1001变量")
                        .withBusinessKey(businessKey)
                        .build());


        System.out.println("启动流程JSON：" + JSON.toJSON(processInstance));
        System.out.println("processInstanceId：" + processInstance.getId());
        System.out.println("processDefinitionKey：" + processInstance.getProcessDefinitionKey());
        System.out.println("businessKey：" + processInstance.getBusinessKey());
    }

    /**
     * 完成任务
     */
    @Test
    public void completeTask() {
        // 业务key
        String businessKey = "demo1001";
        String assignee = "张三";
        HashMap<String, Object> variable = new HashMap<>();
        variable.put("assignee", assignee);
        variable.put("candidateUsers", "李四");
        variable.put("candidateUsers", "王五");
        org.activiti.engine.task.Task task = taskService
                .createTaskQuery()
//                .taskAssignee(assignee)
                .processInstanceBusinessKey(businessKey)
                .singleResult();
        if (task != null) {
            // 有任务时，完成任务
            System.out.println("根据业务key获取任务准备完成：" + task);
            // 完成任务
            CompleteTaskPayload build = TaskPayloadBuilder.complete().withTaskId(task.getId()).withVariables(variable).build();
            Task complete = taskRuntime.complete(build);
            System.out.println("已完成完成任务：" + complete);
        }
    }


}
